<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="培训开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <!-- <j-date :disabled=formDisabled placeholder="请选择开始时间" v-model="model.beginDate"  style="width: 100%" /> -->
              <a-date-picker
                :disabled="formDisabled"
                placeholder="请选择培训开始日期"
                :showToday="false"
                :disabled-date="disableTrainingStartDate"
                valueFormat="YYYY-MM-DD"
                @change="handleChange"
                v-model="model.beginDate"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="培训结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <!-- <j-date :disabled=formDisabled placeholder="请选择结束时间" v-model="model.endDate"  style="width: 100%" /> -->
              <a-date-picker
                :disabled="formDisabled"
                :disabled-date="disabledDate"
                :showToday="false"
                :default-picker-value="this.model.beginDate"
                placeholder="请选择培训结束日期"
                valueFormat="YYYY-MM-DD"
                v-model="model.endDate"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="培训机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="institution">
              <a-input :disabled="formDisabled" v-model="model.institution" placeholder="请输入培训机构" :maxLength="50" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="培训项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="project">
              <a-input :disabled="formDisabled" v-model="model.project" placeholder="请输入培训项目" :maxLength="50" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="获得证书名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificate">
              <a-input :disabled="formDisabled" v-model="model.certificate" placeholder="请输入证书名称" :maxLength="50" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="服务开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceStartDate">
              <j-date
                :disabled="formDisabled"
                placeholder="请选择服务开始日期"
                v-model="model.serviceStartDate"
                date-format="YYYY-MM-DD"
                style="width: 100%"
                @change="handleServiceLimitBack" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="培训服务期限"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="trainingServiceLimit">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.trainingServiceLimit"
                dictCode="training_service_limit"
                placeholder="请选择培训服务期限"
                @input="handleServiceLimitBack" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="服务结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceEndDate">
              <j-date
                disabled
                placeholder="请选择服务结束日期"
                v-model="model.serviceEndDate"
                date-format="YYYY-MM-DD"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="培训费用(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trainingFee">
              <a-input-number
                :disabled="formDisabled"
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="model.trainingFee"
                placeholder="请输入培训费用"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload :disabled="formDisabled" v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { validateBlankValue } from '@/utils/util'
import moment from 'moment';

export default {
  name: 'ErpEmployeeTrainingForm',
  components: {},
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        institution: [
          { required: true, message: '请输入培训机构!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_training', 'institution', value, this.model.id, callback) }
        ],
        serviceStartDate: [
          { required: true, message: '请选择服务开始日期!' }
        ],
        trainingServiceLimit: [
          { required: true, message: '请选择服务期限!' }
        ],
        serviceEndDate: [
          { required: true, message: '请选择服务结束日期!' }
        ],
        trainingFee: [
          { required: true, message: '请输入培训费用!' }
        ],
        attachment: [
          { required: true, message: '请选择附件!' }
        ],
        certificate: [
          { required: true, message: '请输入证书名称!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_training', 'certificate', value, this.model.id, callback) }
        ]
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeeTraining/add',
        edit: '/api/erp/files/employee/erpEmployeeTraining/edit',
        queryById: '/api/erp/files/employee/erpEmployeeTraining/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    moment,
    handleChange(value) {
      if (this.model.endDate) {
        // if ((this.model.endDate).diff((this.model.beginDate), 'days') < 0) {
        //   this.model.endDate = this.model.beginDate
        // }
      }
    },
    // 类型选择
    handleServiceLimitBack() {
      if (this.model.serviceStartDate && this.model.trainingServiceLimit) {
        this.$set(this.model, 'serviceEndDate', moment(this.model.serviceStartDate).add(this.model.trainingServiceLimit, 'day').format('YYYY-MM-DD'))
      } else {
        this.$set(this.model, 'serviceEndDate', '') // 服务结束日期
      }
    },
    disabledDate(current) {
      if (this.model.beginDate) {
        return current && current < moment(this.model.beginDate).endOf('day')
      } else {
        return null
      }
    },
    disableTrainingStartDate(current) {
      if (this.model.endDate) {
        return current >= moment(this.model.endDate).subtract('day')
      } else {
        return null
      }
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record);
      if (!this.model.trainingServiceLimit) {
        this.$set(this.model, 'trainingServiceLimit', '1094')
      }
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          // let httpurl = '';
          // let method = '';
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = 'post';
          // } else {
          //   httpurl += this.url.edit;
          //   method = 'put';
          // }
          // httpAction(httpurl, this.model, method).then((res) => {
          //   if (res.success) {
          //     that.$message.success(res.message);
          //     that.$emit('ok');
          //   } else {
          //     that.$message.warning(res.message);
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false;
          // })
          that.$emit('ok', that.model);
        }
      })
    }
  }
}
</script>
